#!/bin/bash

docker network create --driver bridge --subnet 172.18.0.0/24 --gateway 172.18.0.1 mynet

dockerPath=/opt/docker
zookeeperPath=$dockerPath/zookeeper
zookeeperDataPath=$zookeeperPath/data
zookeeperlogPath=$zookeeperPath/log


mkdir -p $dockerPath
mkdir -p $zookeeperPath
mkdir -p $zookeeperDataPath
mkdir -p $zookeeperlogPath

docker run -d --name zookeeper --restart=always \
-p 2181:2181 \
-v $zookeeperDataPath:/data \
-v $zookeeperlogPath:/datalog \
-e TZ="Asia/Shanghai" \
--network=mynet \
--ip 172.18.0.22 \
zookeeper:3.7.0

#docker exec -it zookeeper /bin/bash
#./bin/zkCli.sh -server localhost:2181

kafkaPath=$dockerPath/kafka
mkdir -p $kafkaPath

kafkaSourceFile=kafka:/opt/kafka/config/server.properties
kafkaTargetFile=$kafkaPath/server.properties

docker run -d --name kafka --restart=always \
-p 9092:9092 \
--env KAFKA_ZOOKEEPER_CONNECT=172.18.0.22:2181 \
--env KAFKA_ADVERTISED_HOST_NAME=172.18.0.30 \
--env KAFKA_ADVERTISED_PORT=9092  \
--network=mynet \
--ip 172.18.0.30 \
wurstmeister/kafka:2.13-2.8.1

sleep 3

docker cp $kafkaSourceFile $kafkaTargetFile

cat >> $kafkaTargetFile <<EOF

#是kafka真正bind的地址
listeners=PLAINTEXT://:9092
#是暴露给外部的listeners，如果没有设置，会用listeners
advertised.listeners=PLAINTEXT://192.168.202.10:9092
#开启Kafka删除Topic
delete.topic.enable=true

EOF

docker cp $kafkaTargetFile $kafkaSourceFile

docker restart kafka

#docker exec -it kafka /bin/bash
#sleep 3
#docker exec -it kafka kafka-topics.sh --create --zookeeper 172.18.0.22:2181 --replication-factor 1 --partitions 2 --topic testInput
#docker exec -it kafka kafka-topics.sh --create --zookeeper 172.18.0.22:2181 --replication-factor 1 --partitions 2 --topic testOutput

##docker exec -it kafka kafka-topics.sh --list --zookeeper 172.18.0.22:2181

##docker exec -it kafka kafka-console-producer.sh --broker-list 172.18.0.30:9092 --topic testInput

##docker exec -it kafka kafka-console-consumer.sh --bootstrap-server 172.18.0.30:9092 --topic testInput --from-beginning
##docker exec -it kafka kafka-console-consumer.sh --bootstrap-server 172.18.0.30:9092 --topic testOutput --from-beginning


docker run -d --name kafka-manager \
--link zookeeper:zookeeper \
--link kafka:kafka -p 9001:9000 \
--restart=always \
--network=mynet \
--ip 172.18.0.31 \
--env ZK_HOSTS=zookeeper:2181 \
sheepkiller/kafka-manager

docker run -d --name zkweb \
-p 8099:8099 \
--restart=always \
--network=mynet \
--ip 172.18.0.23 \
peterpoker/zkweb:v1.2.1

docker run --name mysql-server -t \
-p 3306:3306 \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix" \
      -e MYSQL_ROOT_PASSWORD="zabbix" \
      -d mysql:5.7  \
--character-set-server=utf8 --collation-server=utf8_bin

docker run --name zabbix-java-gateway -t \
      -d zabbix/zabbix-java-gateway:latest

docker run --name zabbix-server-mysql -t \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix" \
      -e MYSQL_ROOT_PASSWORD="zabbix" \
      -e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
      --link mysql-server:mysql \
      --link zabbix-java-gateway:zabbix-java-gateway \
      -p 10051:10051 \
      -d zabbix/zabbix-server-mysql:latest

docker run --name zabbix-web-nginx-mysql -t \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix" \
      -e MYSQL_ROOT_PASSWORD="zabbix" \
      --link mysql-server:mysql \
      --link zabbix-server-mysql:zabbix-server \
      -p 80:80 \
      -d zabbix/zabbix-web-nginx-mysql:latest

##http://192.168.202.10:8080
